package com.ebt.m.customer.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.drawable.RoundDrawable;
import com.ebt.m.data.CustomerData;
import com.ebt.m.utils.DateUtils;

import java.util.Date;
import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Rick.Wang on 2016/4/19.
 */
public class CustomerHeaderViewPagerItem extends RelativeLayout {

    private static final String KEY_NEW = "NEW";       //新建
    private static final String KEY_PROPOSAL = "PROPOSAL";//计划书
    private static final String KEY_INTOUCH = "INTOUCH";   //追踪
    private static final String KEY_TIME = "TIME";   //默认

    private TextView text_view_time;

    private TextView text_view_new;
    private TextView text_view_proposal;
    private TextView text_view_intouch;

    private CustomerData mCustomerData;

    public CustomerHeaderViewPagerItem(Context context) {
        super(context);
        init(context);
    }

    public CustomerHeaderViewPagerItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomerHeaderViewPagerItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context){
        View contentView = inflate(context, R.layout.view_customer_header_view_pager_item, this);
        text_view_time = (TextView)contentView.findViewById(R.id.text_view_time);
        text_view_new = (TextView)contentView.findViewById(R.id.text_view_new);
        text_view_proposal = (TextView)contentView.findViewById(R.id.text_view_proposal);
        text_view_intouch = (TextView)contentView.findViewById(R.id.text_view_intouch);
    }

    //beforeMonth 是当前月份向前数第多少个月，如果beforeMonth == 0，就是当前月份
    public void update(int beforeMonth){
        updateContentByMonthCount(beforeMonth);
//        updateContentByCurSeason();
    }

    public void updateContentByCurSeason(){
        Date[] dates = DateUtils.getDateFromAndDateToByCurSeason();
        updateViews(dates[0], dates[1], "本季度");
    }

    //按照月份计算
    private void updateContentByMonthCount(int beforeMonth) {
        Date[] dates = DateUtils.getDateFromAndDateToByMonthCountBack(beforeMonth);
        updateViews(dates[0], dates[1], getDateTextByBeforeMonth(beforeMonth));
    }

    private String getDateTextByBeforeMonth(int beforeMonth){
        if(beforeMonth == 0){
            return "本月";
        }
        return null;
    }

    //viewpager中的内容自动更新
    private void updateViews(final Date dateFrom, final Date dateTo, String dateText){

        if(mCustomerData == null){
            mCustomerData = new CustomerData(getContext().getApplicationContext());
        }

        Observable
                .create(
                        new ObservableOnSubscribe<HashMap<String, String>>() {
                            @Override
                            public void subscribe(ObservableEmitter<HashMap<String, String>> emitter) throws Exception {
                                String numberOfNew = String.valueOf(mCustomerData.getNumberOfCustomerNewInDateRegion(dateFrom, dateTo));
                                String numberOfSuggestion = String.valueOf(mCustomerData.getNumberOfCustomerProposalInDateRegion(dateFrom, dateTo));
                                String numberOfIntouch = String.valueOf(mCustomerData.getNumberOfCustomerIntouchInDateRegion(dateFrom, dateTo));
                                HashMap<String, String> hashMap = new HashMap<String, String>();
                                hashMap.put(KEY_NEW, numberOfNew);
                                hashMap.put(KEY_INTOUCH, numberOfIntouch);
                                hashMap.put(KEY_PROPOSAL, numberOfSuggestion);
                                hashMap.put(KEY_TIME, dateText == null ? DateUtils.dateTime2String(dateFrom, "yyyy年M月") : dateText);
                                emitter.onNext(hashMap);
                                emitter.onComplete();
                            }
                        })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<HashMap<String,String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(HashMap<String, String> hashMap) {
                        String timeTitle = hashMap.get(KEY_TIME);
                        String numberOfNew = hashMap.get(KEY_NEW);
                        String numberOfIntouch = hashMap.get(KEY_INTOUCH);
                        String numberOfProposal = hashMap.get(KEY_PROPOSAL);

                        text_view_time.setText(timeTitle);
                        setSpannableTextAndBgForTVNew(text_view_new, numberOfNew + "人");
                        setSpannableTextAndBgForTVNew(text_view_proposal, numberOfProposal + "人");
                        setSpannableTextAndBgForTVNew(text_view_intouch, numberOfIntouch + "人");

                        /*setSpannableTextAndBgForTV(text_view_new, numberOfNew + "人\n", POSTFIX_NEW, FlagView.COLORS[0]);
                        setSpannableTextAndBgForTV(text_view_proposal, numberOfProposal + "人\n", POSTFIX_PROPOSAL, FlagView.COLORS[1]);
                        setSpannableTextAndBgForTV(text_view_intouch, numberOfIntouch + "人\n", POSTFIX_INTOUCH, FlagView.COLORS[2]);*/
                    }
                });
    }

    private void setSpannableTextAndBgForTV(TextView textView, String string0, String string1, int color){
        SpannableString spanText = new SpannableString(string0 + string1);
        spanText.setSpan(new TextAppearanceSpan(getContext(),
                R.style.customer_header_textview_large), 0, string0.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new TextAppearanceSpan(getContext(),
                R.style.customer_header_textview_small), string0.length(), spanText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spanText, TextView.BufferType.SPANNABLE);

        textView.setBackgroundDrawable(new RoundDrawable(new ColorDrawable(color)));
    }

    private void setSpannableTextAndBgForTVNew(TextView textView, String string0){
        SpannableString spanText = new SpannableString(string0);
        spanText.setSpan(new TextAppearanceSpan(getContext(),
                R.style.customer_header_textview_large), 0, string0.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spanText, TextView.BufferType.SPANNABLE);

//        textView.setBackgroundDrawable(new RoundDrawable(new ColorDrawable(color)));
    }


}